using System;
using System.IO;
using System.Web.UI.HtmlControls;
using miPics.Common;
using miPics.Rss;


public partial class _Administration_DeleteImage : miPics.BasePage.BasePage
{
	private bool ValidImage = false;
	private short GalleryID;
	private short ImageID;
	private string ThumbnailImage, FullsizeImage;

	protected void Page_Load(object sender, EventArgs e)
	{
		// set menu administration task
		Master.SetMenuAdministrationTask("Delete image");

		// load image we are going to delete
		this.ImageID = ToolKit.ReturnShort(Request.QueryString["id"]);

		// get image details into datatable through table adapter
		miPicsDataAccessTableAdapters.ImageItemTableAdapter ImageItemTableAdapter = new miPicsDataAccessTableAdapters.ImageItemTableAdapter();
		miPicsDataAccess.ImageItemDataTable ImageItemDataTable = ImageItemTableAdapter.GetData(this.ImageID, this.IsLoggedIn);

		// if no image loaded show error message
		this.ImageLoaded = (ImageItemDataTable.Count == 1);

		// was a image record found?
		if (ImageItemDataTable.Count == 1)
		{
			this.ValidImage = true;

			// place image record into item row object
			miPicsDataAccess.ImageItemRow ImageItemRow = (miPicsDataAccess.ImageItemRow)ImageItemDataTable.Rows[0];

			// save galleryid, thumbnail and fullsize image
			this.GalleryID = ImageItemRow.GalleryID;
			this.ThumbnailImage = ImageItemRow.Thumbnail;
			this.FullsizeImage = ImageItemRow.Fullsize;

			// if not postback, populate fields with image data
			if (!this.IsPostBack)
			{
				// pass image details to control
				_Image.Src = "~/_Resource/Image/" + ImageItemRow.Thumbnail;
				_Image.Alt = ToolKit.GetAltImageName(ImageItemRow.Name);
				_Image.Width = ImageItemRow.ThumbnailWidth;
				_Image.Height = ImageItemRow.ThumbnailHeight;

				// pass image desc details
				this.SetImageDescProperty(_ImageName, ImageItemRow.Name);
				this.SetImageDescProperty(_ImageDescription, ImageItemRow.Description);
			}
		}	
	}

	protected void DeleteImage(object sender, EventArgs e)
	{
		if (this.ValidImage)
		{
			// delete images and image record from database
			string ImageFolder = Request.PhysicalApplicationPath + "_Resource\\Image\\";

			// delete thumbnail image
			ToolKit.DeleteFile(ImageFolder + this.ThumbnailImage);

			// delete fullsize image
			ToolKit.DeleteFile(ImageFolder + this.FullsizeImage);

			// delete database record
			miPicsDataAccessTableAdapters.ImageItemTableAdapter ImageItemTableAdapter = new miPicsDataAccessTableAdapters.ImageItemTableAdapter();
			ImageItemTableAdapter.DeleteItem(this.ImageID);

			// update gallery rss
			WriteRss GalleryRss = new WriteRss(this.Request);
			GalleryRss.SaveRss();

			// redirect back to the gallery
			this.RedirectToGallery();
		}
	}

	protected void CancelDelete(object sender, EventArgs e)
	{
		this.RedirectToGallery();
	}

	private void RedirectToGallery()
	{
		if (this.ValidImage)
			Response.Redirect("~/Gallery/?id=" + this.GalleryID.ToString());
	}

	private void SetImageDescProperty(HtmlGenericControl PropertyControl,string DescProperty)
	{
		if (DescProperty.Trim() == "")
		{
			PropertyControl.InnerText = "None";
			PropertyControl.Attributes["class"] = "none";
		}
		else
		{
			PropertyControl.InnerText = DescProperty;
		}
	}

	private bool ImageLoaded
	{
		set
		{
			_ErrorMessage.Visible = !value;
			_Interface.Visible = value;
		}
	}
}